package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.PrizeDrawActivityAwardDao;
import com.sc.nft.dao.PrizeDrawActivityDao;
import com.sc.nft.dto.ActivityListEditDTO;
import com.sc.nft.entity.PrizeDrawActivity;
import com.sc.nft.entity.PrizeDrawActivityAward;
import com.sc.nft.entity.dto.AcquisitionActivityDTO;
import com.sc.nft.entity.dto.ActivityApproveDTO;
import com.sc.nft.entity.dto.ActivityListDTO;
import com.sc.nft.entity.vo.AcquisitionActivityVO;
import com.sc.nft.entity.vo.PrizeDrawActivityAwardDetailsVO;
import com.sc.nft.entity.vo.PrizeDrawActivityDetailsVO;
import com.sc.nft.entity.vo.PrizeDrawActivityVO;
import com.sc.nft.service.PrizeDrawActivityService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 抽奖活动(PrizeDrawActivity)表服务实现类
 *
 * @author wwc
 * @since 2023-08-22 14:06:27
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class PrizeDrawActivityServiceImpl implements PrizeDrawActivityService {

    private final PrizeDrawActivityDao prizeDrawActivityDao;

    private final PrizeDrawActivityAwardDao prizeDrawActivityAwardDao;

    @Override
    public Page<PrizeDrawActivityVO> activityList(ActivityListDTO activityListDTO) {
        return prizeDrawActivityDao.activityList(activityListDTO);
    }

    @Override
    public void approve(ActivityApproveDTO activityApproveDTO) {
        PrizeDrawActivity prizeDrawActivity = prizeDrawActivityDao.getById(activityApproveDTO.getId());
        if (BeanUtil.isEmpty(prizeDrawActivity)) {
            throw new RuntimeException("活动不存在");
        }
        if (prizeDrawActivity.getAuditStatus() == 1) {
            throw new RuntimeException("活动已通过");
        }
        if (prizeDrawActivity.getAuditStatus() != 0) {
            throw new RuntimeException("当前状态不能审批");
        }
        prizeDrawActivityDao.approve(activityApproveDTO);
    }

    @Override
    public PrizeDrawActivityDetailsVO getById(Long id) {
        PrizeDrawActivityDetailsVO prizeDrawActivityDetailsVO = new PrizeDrawActivityDetailsVO();
        PrizeDrawActivity byId = prizeDrawActivityDao.getById(id);
        BeanUtil.copyProperties(byId, prizeDrawActivityDetailsVO);
        List<PrizeDrawActivityAward> prizeDrawActivityAwards = prizeDrawActivityAwardDao.getByPrizeDrawActivityId(byId.getId());
        List<PrizeDrawActivityAwardDetailsVO> list = BeanUtil.copyToList(prizeDrawActivityAwards, PrizeDrawActivityAwardDetailsVO.class);
        prizeDrawActivityDetailsVO.setList(list);
        return prizeDrawActivityDetailsVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(ActivityListEditDTO activityListEditDTO) {
        /*
        1.根据id查询活动
        2.判断活动是否存在
        3.已通过的活动不能编辑
         */
        PrizeDrawActivity prizeDrawActivity = prizeDrawActivityDao.getById(activityListEditDTO.getId());
        if (BeanUtil.isEmpty(prizeDrawActivity)) {
            throw new RuntimeException("活动不存在");
        }
        PrizeDrawActivity editData = BeanUtil.copyProperties(activityListEditDTO, PrizeDrawActivity.class);
        editData.updateById();
    }

    @Override
    public IPage<AcquisitionActivityVO> acquisitionActivity(AcquisitionActivityDTO acquisitionActivityDTO) {
        IPage<PrizeDrawActivity> prizeDrawActivityIPage = prizeDrawActivityDao.acquisitionActivity(acquisitionActivityDTO);
        return prizeDrawActivityIPage.convert(prizeDrawActivity -> {
            AcquisitionActivityVO acquisitionActivityVO = new AcquisitionActivityVO();
            BeanUtil.copyProperties(prizeDrawActivity, acquisitionActivityVO);
            // 当前时间大于开始时间，小于结束时间，活动状态为1
            if (System.currentTimeMillis() > prizeDrawActivity.getStartTime().getTime() && System.currentTimeMillis() < prizeDrawActivity.getEndTime().getTime()) {
                acquisitionActivityVO.setActivityStatus(1);
            }
            // 当前时间大于结束时间，活动状态为2
            if (System.currentTimeMillis() > prizeDrawActivity.getEndTime().getTime()) {
                acquisitionActivityVO.setActivityStatus(2);
            }
            // 当前时间小于开始时间，活动状态为0
            if (System.currentTimeMillis() < prizeDrawActivity.getStartTime().getTime()) {
                acquisitionActivityVO.setActivityStatus(0);
            }
            return acquisitionActivityVO;
        });
    }

    @Override
    public AcquisitionActivityVO getByIdDetails(Long id) {
        PrizeDrawActivity prizeDrawActivity = prizeDrawActivityDao.getById(id);
        AcquisitionActivityVO acquisitionActivityVO = BeanUtil.copyProperties(prizeDrawActivity, AcquisitionActivityVO.class);
        if (System.currentTimeMillis() > prizeDrawActivity.getStartTime().getTime() && System.currentTimeMillis() < prizeDrawActivity.getEndTime().getTime()) {
            acquisitionActivityVO.setActivityStatus(1);
        }
        // 当前时间大于结束时间，活动状态为2
        if (System.currentTimeMillis() > prizeDrawActivity.getEndTime().getTime()) {
            acquisitionActivityVO.setActivityStatus(2);
        }
        // 当前时间小于开始时间，活动状态为0
        if (System.currentTimeMillis() < prizeDrawActivity.getStartTime().getTime()) {
            acquisitionActivityVO.setActivityStatus(0);
        }
        return acquisitionActivityVO;
    }

}
